#define _CRT_SECURE_NO_WARNINGS

#include <stdbool.h>
#include <stdio.h>
struct ListNode
{
    int val;
    struct ListNode* next;
};
//typedef struct ListNode ListNode;
//int getlength(ListNode* head)
//{
//    int ret = 0;
//    ListNode* current = head;
//    for (; current;)
//    {
//        current = current->next;
//        ret++;
//    }
//    return ret;
//}
//
//struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
//{
//    int lenA = getlength(headA);
//    int lenB = getlength(headB);
//    int i = 0;
//    ListNode* Long = (lenA > lenB || lenA == lenB ? headA : headB);
//    ListNode* Short = (lenA < lenB ? headA : headB);
//    for (; i < abs(lenA - lenB); i++)
//    {
//        Long = Long->next;
//    }
//    for (; Long && Short;)
//    {
//        if (Long == Short)
//        {
//            return Long;
//        }
//        Long = Long->next;
//        Short = Short->next;
//    }
//    return NULL;
//}
//typedef struct ListNode LN;
//bool hasCycle(struct ListNode* head)
//{
//    LN* fast = head;
//    LN* slow = head;
//    for (; fast && fast->next;)
//    {
//        fast = fast->next->next;
//        slow = slow->next;
//        if (fast == slow)
//        {
//            return true;
//        }
//    }
//    return false;
//}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode LN;
LN* detectCycle(struct ListNode* head)
{
    LN* fast = head;
    LN* slow = head;
    LN* current = head;
    for (; fast && fast->next;)
    {
        fast = fast->next->next;
        slow = slow->next;
        if (fast == slow)
        {
            for (;;)
            {
                if (current == fast)
                {
                    return fast;
                }
                current = current->next;
                fast = fast->next;
            }
        }
    }

    return NULL;
}